package org.acghub.mtdb.core.log.impl;

import org.acghub.mtdb.core.LogKey;
import org.acghub.mtdb.core.log.LogAbstractMap;

import java.util.*;

public final class LogTreeMap<K, V> extends LogAbstractMap<K, V> implements NavigableMap<K, V> {

    public LogTreeMap(LogKey logkey, NavigableMap<K, V> wrapped) {
        super(logkey, wrapped);
    }

    private LogTreeMap(LogTreeMap<K, V> root, NavigableMap<K, V> wrapped) {
        super(root, wrapped);
    }

    private LogTreeMap(LogTreeMap<K, V> root, SortedMap<K, V> wrapped) {
        super(root, wrapped);
    }


    protected NavigableMap<K, V> wrapped() {
        return (NavigableMap<K, V>) wrapped;
    }

    @Override
    public Entry<K, V> lowerEntry(K key) {
        return new LogEntry(wrapped().lowerEntry(key));
    }

    @Override
    public K lowerKey(K key) {
        return wrapped().lowerKey(key);
    }

    @Override
    public Entry<K, V> floorEntry(K key) {
        return new LogEntry(wrapped().floorEntry(key));
    }

    @Override
    public K floorKey(K key) {
        return wrapped().floorKey(key);
    }

    @Override
    public Entry<K, V> ceilingEntry(K key) {
        return new LogEntry(wrapped().ceilingEntry(key));
    }

    @Override
    public K ceilingKey(K key) {
        return wrapped().ceilingKey(key);
    }

    @Override
    public Entry<K, V> higherEntry(K key) {
        return new LogEntry(wrapped().higherEntry(key));
    }

    @Override
    public K higherKey(K key) {
        return wrapped().higherKey(key);
    }

    @Override
    public Entry<K, V> firstEntry() {
        return new LogEntry(wrapped().firstEntry());
    }

    @Override
    public Entry<K, V> lastEntry() {
        return new LogEntry(wrapped().lastEntry());
    }

    @Override
    public Entry<K, V> pollFirstEntry() {
        return new LogEntry(wrapped().pollFirstEntry());
    }

    @Override
    public Entry<K, V> pollLastEntry() {
        return new LogEntry(wrapped().pollLastEntry());
    }

    @Override
    public NavigableMap<K, V> descendingMap() {
        return new LogTreeMap<>(this, wrapped().descendingMap());
    }

    @Override
    public NavigableSet<K> navigableKeySet() {
        // TODO: 待实现
        return Collections.unmodifiableNavigableSet(wrapped().navigableKeySet());
    }

    @Override
    public NavigableSet<K> descendingKeySet() {
        //  TODO: 待实现
        return Collections.unmodifiableNavigableSet(wrapped().descendingKeySet());
    }

    @Override
    public NavigableMap<K, V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
        return new LogTreeMap<>(this, wrapped().subMap(fromKey, fromInclusive, toKey, toInclusive));
    }

    @Override
    public NavigableMap<K, V> headMap(K toKey, boolean inclusive) {
        return new LogTreeMap<>(this, wrapped().headMap(toKey, inclusive));
    }

    @Override
    public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive) {
        return new LogTreeMap<>(this, wrapped().tailMap(fromKey, inclusive));
    }

    @Override
    public Comparator<? super K> comparator() {
        return sortedWrapped().comparator();
    }

    @Override
    public SortedMap<K, V> subMap(K fromKey, K toKey) {
        return new LogTreeMap<>(this, sortedWrapped().subMap(fromKey, toKey));
    }

    @Override
    public SortedMap<K, V> headMap(K toKey) {
        return new LogTreeMap<>(this, sortedWrapped().headMap(toKey));
    }

    @Override
    public SortedMap<K, V> tailMap(K fromKey) {
        return new LogTreeMap<>(this, sortedWrapped().tailMap(fromKey));
    }

    @Override
    public K firstKey() {
        return wrapped().firstKey();
    }

    @Override
    public K lastKey() {
        return wrapped().lastKey();
    }

    SortedMap<K, V> sortedWrapped() {
        return (SortedMap<K, V>) wrapped;
    }

}
